package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dao.SetmealDto;
import com.qf.entity.Category;
import com.qf.entity.Setmeal;
import com.qf.entity.SetmealDish;
import com.qf.mapper.SetmealMapper;
import com.qf.service.CategoryService;
import com.qf.service.SetmealDishService;
import com.qf.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;


    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        //往套餐的菜品集合里面设置套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> findPage(Integer page, Integer pageSize, String name) {

        //查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        if (name != null && name != "") {
            //去掉前后空格
            name = name.trim();
            wrapper.like(Setmeal::getName, name);
        }
        //展示没有删除的条件
        wrapper.eq(Setmeal::getIsDeleted, 0);
        //添加排序条件，根据更新时间降序排列
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        Page<Setmeal> page1 = this.page(pageInfo, wrapper);
        //判断
        if (page1 == null) {
            return null;
        }
        //再次封装属性,获得所有的套餐信息
        List<Setmeal> records = page1.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();
        //遍历所有套餐对象那个,转成一个dto
        records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //查询分类的名字
            Category byId = categoryService.getById(setmealDto.getCategoryId());
            setmealDto.setCategoryName(byId.getName());
            setmealDtos.add(setmealDto);
            return item;
        }).collect(Collectors.toList());

        Page<SetmealDto> dtoPage = new Page<>();
        dtoPage.setRecords(setmealDtos);
        dtoPage.setTotal(page1.getTotal());
        return dtoPage;
    }


    /**
     * 批量删除套餐数据
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public boolean deleted(Long[] ids) {
        //逻辑删除,先把数组转成集合
        List<Long> longs = Arrays.asList(ids);
        //调用方法删除集合
        List<Setmeal> setmeals = this.listByIds(longs);
        //循环修改套餐的删除属性就是删除
        setmeals = setmeals.stream().map(c -> {
            c.setIsDeleted(1);
            return c;
        }).collect(Collectors.toList());
        //修改
        this.updateBatchById(setmeals);

        //修改套餐菜品信息
        for (Setmeal setmeal : setmeals) {
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> list = setmealDishService.list(wrapper);
            list = list.stream().map(c -> {
                c.setIsDeleted(1);
                return c;
            }).collect(Collectors.toList());
            setmealDishService.updateBatchById(list);
        }
        //执行成功
        return true;
    }

    /**
     * 批量起售,停售
     *
     * @param status
     * @param ids
     * @return
     */
    @Override
    public boolean updateByIds(Integer status, Long[] ids) {
        //转换数据,得到集合
        List<Long> longs = Arrays.asList(ids);
        //调用方法修改状态
        List<Setmeal> setmeals = this.listByIds(longs);
        setmeals = setmeals.stream().map(c -> {
            c.setStatus(status);
            return c;
        }).collect(Collectors.toList());
        boolean byId = this.updateBatchById(setmeals);
        if (byId == false) {
            return false;
        }
        return true;
    }

    /**
     * 回显套餐数据
     *
     * @param id
     * @return
     */
    @Override
    public SetmealDto findupdate(Long id) {
        //用本类方法查询setmeal表的数据
        Setmeal setmeal = this.getById(id);
        //封装到dto
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);

        //查询当前套餐素有的菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        //套餐关系表当中的套餐id对应的是套餐当中的id
        wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
        //list集合里面是该套餐里的所有菜品
        List<SetmealDish> list = setmealDishService.list(wrapper);
        //将菜品封装到dto
        if (list == null) {
            //没有查到菜品
            return null;
        }
        //封装并返回
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐数据
     *
     * @param setmealDto
     * @return
     */
    @Override
    @Transactional
    public boolean updateDto(SetmealDto setmealDto) {
        //首先修改setmeal信息
        this.updateById(setmealDto);

        //清理当前套餐的菜品,根据套餐的id删除菜品关系表
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(queryWrapper);

        //再修改套餐菜品表信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((c) -> {
            //将菜品表当中重新添加该套餐id
            c.setSetmealId(setmealDto.getId());
            return c;
        }).collect(Collectors.toList());
        boolean b = setmealDishService.saveBatch(setmealDishes);
        //判断是否修改完成
        if (b) {
            return true;
        }
        return false;
    }
}
